package editors.objectedit;
import main.Config;
import main.ProgramLauncher;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

import tools.Help;
import tools.Point;
import editors.gui.GuiButton;
import editors.gui.GuiHandler;
import editors.gui.GuiLabel;
import editors.gui.GuiSettings;
import editors.gui.GuiText;
import graphics.effects.Compositor;
import graphics.effects.Layer;
import graphics.effects.PartSys;
import graphics.synchronize.GSyncTools;
import graphics.system.Color;
import graphics.system.GLTools;
import graphics.system.Renderer;
import graphics.system.TextureContainer;
public class EditorSelecterRenderer extends Renderer {
	private GuiHandler gui;
	private GuiHandler levelname;
	private boolean startgame;
	public EditorSelecterRenderer() {
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glShadeModel(GL11.GL_SMOOTH);
		GL11.glDisable(GL11.GL_LIGHTING);
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		GL11.glEnable(GL11.GL_LINE_SMOOTH);
		GL11.glEnable(GL11.GL_COLOR_MATERIAL);
		width = Config.RESOLUTION.x;
		height = Config.RESOLUTION.y;
		this.setCameraPosition(new Point(0, 0));
		this.zoom = 1;
		TextureContainer.loadTextures();
		// PartSys.init();
		// Layer.init();
		// GSyncTools.init();
		// Compositor.init();
		GLTools.init();
		startgame = false;
		gui = new GuiHandler();
		Point g = new Point(0, 0);
		gui.addGuiObject("title", new GuiLabel(g, 200, "rohXel-Editors"));
		g.y -= GuiSettings.LINEHEIGHT + 3;
		gui.addGuiObject("object", new GuiButton(g, 200, "Object Editor"));
		g.y -= GuiSettings.LINEHEIGHT + 3;
		gui.addGuiObject("waypoint", new GuiButton(g, 200, "Waypoint Editor"));
		g.y -= GuiSettings.LINEHEIGHT + 3;
		gui.addGuiObject("game", new GuiButton(g, 200, "Start Game"));
		g.y -= GuiSettings.LINEHEIGHT + 3;
		gui.addGuiObject("exit", new GuiButton(g, 200, "Exit"));
		levelname = new GuiHandler();
		g = new Point(0, 0);
		levelname.addGuiObject("label", new GuiLabel(g, 150, "Levelname:"));
		g.y -= GuiSettings.LINEHEIGHT + 3;
		levelname.addGuiObject("lname", new GuiText(g, 150));
		g.y -= GuiSettings.LINEHEIGHT + 3;
		levelname.addGuiObject("start", new GuiButton(g.add(60, 0), 40, "Start"));
	}
	public void handle() {
		control();
		render();
	}
	public void render() {
		GL11.glClearColor(GuiSettings.GUI_BG.r, GuiSettings.GUI_BG.g, GuiSettings.GUI_BG.b, 0);
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		gui.moveTo(new Point(0, height));
		levelname.moveTo(new Point(0, height));
		if (!startgame)
			gui.drawAll();
		else
			levelname.drawAll();
	}
	private void control() {
		while (Mouse.next()) {
			if (Mouse.getEventButtonState() && Mouse.getEventButton() == 0) {
				// gui.setClick(new Point(Mouse.getX(),Mouse.getY()));
				if (!startgame)
					gui.handleClick(new Point(Mouse.getX(), Mouse.getY()));
				else
					levelname.handleClick(new Point(Mouse.getX(), Mouse.getY()));
				// Help.p("Click"+new Point(Mouse.getX(),Mouse.getY()));
			}
			else {
				gui.unClickAll();
				levelname.unClickAll();
			}
		}
		if (!startgame) {
			if (gui.isClicked("game")) {
				startgame = true;
			}
			else if (gui.isClicked("object")) {
				ProgramLauncher.startObjectEditor();
			}
			else if (gui.isClicked("waypoint")) {
				ProgramLauncher.startWaypointEditor();
			}
			else if (gui.isClicked("exit")) {
				System.exit(0);
			}
		}
		else {
			if (levelname.inTextEditMode()) {
				while (Keyboard.next()) {
					if (Keyboard.getEventKeyState()) {
						levelname.applyKeyPress(Keyboard.getEventCharacter());
					}
					if (Keyboard.getEventKeyState() && Keyboard.getEventKey() == Keyboard.KEY_BACK) {
						levelname.deleteChar();
					}
				}
			}
			if (levelname.isClicked("start")) {
				ProgramLauncher.startServer(levelname.getText("lname"));
			}
		}
	}
}
